class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int n = gas.size();
        int ret = 0, g = 0, ming = 0;
        for(int i = 0; i < n; ++i)
        {
            g += gas[i] - cost[i];
            if(ming > g)
            {
                ret = i + 1;
                ming = g;
            }
        }
        return g < 0 ? -1 : ret;
    }
};
